package com.lzw.cpvideoplayer.mvp.view;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.AnimationSet;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;

import com.lzw.cpvideoplayer.R;


import androidx.annotation.NonNull;

public class LoadDiaLog extends Dialog  {
    private static final String TAG = "LoadDiaLog";
      private ImageView dig_img;
      private static Context mContext;
      private AnimationSet animationSet;
      private static LoadDiaLog instance;
      public LoadDiaLog(@NonNull Context context) {
         super(context);

      }
      public static  LoadDiaLog getInstance(Context context){

              if(instance == null) {
                  instance = new LoadDiaLog(context);
              }

         return instance;


      }
      @Override
      protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         //透明化处理
         getWindow().requestFeature(Window.FEATURE_NO_TITLE);
         getWindow().setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN,
                 WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN);
         getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));//透明背景
         getWindow().setDimAmount(0f);
         this.setContentView(R.layout.dialog_loading);
         //设置dialog属性
         setCancelable(true);
         setCanceledOnTouchOutside(false);

         dig_img = findViewById(R.id.dig_img);
         loadImg();

      }
      @Override
      protected void onStart() {
         super.onStart();
         dig_img.startAnimation(animationSet);//开始播放
          Log.e(TAG, "onStart: " );
      }

      @Override
      protected void onStop() {
         super.onStop();
      }

      private void loadImg() {
         animationSet = new AnimationSet(true);
         RotateAnimation animation_rotate = new RotateAnimation(0, +359,
                 RotateAnimation.RELATIVE_TO_SELF, 0.5f,
                 RotateAnimation.RELATIVE_TO_SELF, 0.5f);
         //第一个参数fromDegrees为动画起始时的旋转角度 //第二个参数toDegrees为动画旋转到的角度
         //第三个参数pivotXType为动画在X轴相对于物件位置类型 //第四个参数pivotXValue为动画相对于物件的X坐标的开始位置
         //第五个参数pivotXType为动画在Y轴相对于物件位置类型 //第六个参数pivotYValue为动画相对于物件的Y坐标的开始位置
         animation_rotate.setRepeatCount(-1);
         animation_rotate.setStartOffset(0);
         animation_rotate.setDuration(1200);
         LinearInterpolator lir = new LinearInterpolator();
         animationSet.setInterpolator(lir);
         animationSet.addAnimation(animation_rotate);

      }

       public void onLoadDismiss(){
            this.dismiss();
            instance = null;
        }

   }

